package org.com.yz.common.core;

import com.alibaba.fastjson.JSONObject;
import org.com.yz.common.util.DateUtils;

import java.util.HashMap;

/**
 * Created by lsk on 2018-6-21.
 */
public class EPay {

    /**
     * 智能代付单笔付款接口
     *
     * @param order_no
     *            订单号
     * @param to_bank_no
     *            收款行行号
     * @param to_acct_no
     *            收款人账户
     * @param to_acct_name
     *            收款人户名
     * @param acct_type
     *            账户类型：0-储蓄卡,1-信用卡,2-对公账户
     * @param trans_amt
     *            付款金额
     * @param trans_usage
     *            用途
     * @return json格式结果，返回结果包含字段请参看收付直通车代收接口文档
     */
    public static String pyPay(String mchNo,String order_no, String to_bank_no, String to_acct_no, String to_acct_name,
                               String acct_type, String trans_amt, String trans_usage) {

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("order_no", order_no);
        params.put("to_bank_no", to_bank_no);
        params.put("to_acct_no", to_acct_no);
        params.put("to_acct_name", to_acct_name);
        params.put("acct_type", acct_type);
        params.put("trans_amt", trans_amt);
        params.put("trans_usage", trans_usage);

        return new PyPay().exec(params,mchNo);
    }

    public static String pyPay(String mchNo,String order_no, String to_bank_no, String to_acct_no, String to_acct_name,
                               String acct_type, String trans_amt, String trans_usage,String bsbank_no,String bsbank_name) {

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("order_no", order_no);
        params.put("to_bank_no", to_bank_no);
        params.put("to_acct_no", to_acct_no);
        params.put("to_acct_name", to_acct_name);
        params.put("acct_type", acct_type);
        params.put("trans_amt", trans_amt);
        params.put("trans_usage", trans_usage);
        params.put("bsbank_no",bsbank_no);
        params.put("bsbank_name",bsbank_name);

        return new PyPay().exec(params,mchNo);
    }


    public static void main(String[] args) throws InterruptedException {
        String acct_type = "0"; // 账户类型-借记卡
        String mch_no = "80000004";
        String order_no = "SDK" + DateUtils.getCurrentTimeStrDefault(); // 这里示例使用SDK20150806120001格式的订单号
        String trans_amt = "0.02"; // 支付金额
//        String to_bank_no = "309391000011"; // 收款行行号
        String to_bank_no = "103100000026";
        String to_acct_no = "62284807691010783761"; // 收款人账号
        String to_acct_name = "冷朝"; // 收款人户名
        String trans_usage = "这笔订单是由SDK发起的示例订单"; // 订单详情

        String ex3_0result = pyPay(mch_no,order_no, to_bank_no, to_acct_no, to_acct_name, acct_type, trans_amt,
                trans_usage);
        System.out.println(ex3_0result);

        JSONObject obj = JSONObject.parseObject(ex3_0result);
        String transStatus = (String) obj.get("transStatus");
        int i = 1;
        while ("3".equals(transStatus)){ //transStatus 1：成功 2：失败 3：未决
            System.out.println("交易后第"+i+"次查询");

            ex3_0result = EPay.pyQuery(mch_no,order_no);
            System.out.println(ex3_0result);
            obj = JSONObject.parseObject(ex3_0result);
            transStatus = (String) obj.get("transStatus");

            Thread.sleep(3000); //休眠3秒钟

            i++;
        }

        if("1".equals(transStatus)){
            System.out.println("交易成功");
        }else if("2".equals(transStatus)){
            System.out.println("交易失败");
        }
    }
    /**
     * 智能代付单笔订单查询接口
     *
     * @param order_no
     *            订单号
     * @return json格式结果，返回结果包含字段请参看收付直通车代收接口文档
     */
    public static String pyQuery(String order_no,String mchNo) {

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("order_no", order_no);

        return new PyQuery().exec(params,mchNo);
    }

}
